package com.chinatelecom.ctyun.xstore.vod.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.chinatelecom.ctyun.xstore.vod.demo.client.XstoreVodClient;
import com.ctyun.xstore.vod.template.model.CreateTranscodeTemplateRequest;
import com.ctyun.xstore.vod.template.model.CreateWatermarkTemplateRequest;
import com.ctyun.xstore.vod.template.model.SearchTranscodeTemplateRequest;
import com.ctyun.xstore.vod.template.model.SearchWatermarkTemplateRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping(value = "/template")
public class TemplateDemoController {

	@Autowired
	private XstoreVodClient xstoreVodClient;

	/**
	 * 创建一个转码模板
	 *
	 * @param paramObject 创建模板的参数
	 * @return 创建模板的结果，注意：该结果仅返回 templateId，其他字段均为空
	 * @throws IOException 可能会发生 IO 异常。
	 */
	@PostMapping(value = "/transcode/create")
	public Object createTranscodeTemplate(@RequestBody JSONObject paramObject) throws IOException {
		CreateTranscodeTemplateRequest request = paramObject.toJavaObject(CreateTranscodeTemplateRequest.class);
		return xstoreVodClient.getTranscodeTemplateClient().createTranscodeTemplate(request);
	}

	/**
	 * 搜索转码模板
	 *
	 * @param paramObject
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "/transcode/search")
	public Object searchTranscodeTemplate(@RequestBody JSONObject paramObject) throws IOException {
		SearchTranscodeTemplateRequest request = paramObject.toJavaObject(SearchTranscodeTemplateRequest.class);
		return xstoreVodClient.getTranscodeTemplateClient().searchTranscodeTemplate(request);
	}

	/**
	 * 获取单个转码模板
	 *
	 * @param templateId
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "/transcode/get")
	public Object getTranscodeTemplate(Long templateId) throws IOException {
		return xstoreVodClient.getTranscodeTemplateClient().getTranscodeTemplate(templateId);
	}

	/**
	 * 删除转码模板
	 *
	 * @param templateId
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "/transcode/delete")
	public Object deleteTranscodeTemplate(Long templateId) throws IOException {
		return xstoreVodClient.getTranscodeTemplateClient().deleteTranscodeTemplate(templateId);
	}

	/**
	 * 创建水印模板
	 *
	 * @param paramObject 创建转码模板请求CreateWatermarkTemplateRequest的实体类
	 * @return CreateWatermarkTemplateResponse, 创建水印模板返回类，仅包含templateId
	 * @throws IOException 可能抛出IO异常
	 */
	@PostMapping(value = "/watermark/create")
	public Object createWatermarkTemplate(@RequestBody JSONObject paramObject) throws IOException {
		CreateWatermarkTemplateRequest request = paramObject.toJavaObject(CreateWatermarkTemplateRequest.class);
		return xstoreVodClient.getWatermarkTemplateClient().createWatermarkTemplate(request);
	}

	/**
	 * 上传水印图片
	 *
	 * @param presignedUrl 水印图片上传的预签名url
	 * @param filePath 水印图片文件本地路径
	 * @return
	 * @throws java.net.MalformedURLException presignedUrl不符合规范
	 * @throws Exception 其他异常
	 */
	@PostMapping(value = "/watermark/upload")
	public void uploadWatermarkPicture(String presignedUrl, String filePath) throws Exception {
		xstoreVodClient.getWatermarkTemplateClient().uploadWatermarkPicture(presignedUrl, filePath);
	}

	/**
	 * 完成水印图片上传
	 *
	 * @param templateId 指定水印模板的id
	 * @return CompleteWatermarkTemplateResponse, 完成水印图片上传的返回类，只包含code和message
	 * @throws IOException 可能抛出IO异常
	 */
	@PostMapping(value = "/watermark/complete")
	public Object completeWatermarkTemplate(String templateId) throws IOException {
		return xstoreVodClient.getWatermarkTemplateClient().completeWatermarkTemplate(templateId);
	}

	/**
	 * 搜索水印模板
	 *
	 * @param paramObject 搜索水印模板请求SearchWatermarkTemplateRequest的实体类
	 * @return SearchWatermarkTemplateResponse, 搜索水印模板返回类，包含一个JSONArray，成员为WatermarkTemplate实体类
	 * @throws IOException 可能抛出IO异常
	 */
	@PostMapping(value = "/watermark/search")
	public Object searchWatermarkTemplate(@RequestBody JSONObject paramObject) throws IOException {
		SearchWatermarkTemplateRequest request = paramObject.toJavaObject(SearchWatermarkTemplateRequest.class);
		return xstoreVodClient.getWatermarkTemplateClient().searchWatermarkTemplate(request);
	}

	/**
	 * 获取单个水印模板
	 *
	 * @param templateId 指定获取的水印模板的id
	 * @return GetWatermarkTemplateResponse, 获取单个水印模板返回类, 包含一个WatermarkTemplate
	 * @throws IOException 可能抛出IO异常
	 */
	@PostMapping(value = "/watermark/get")
	public Object getWatermarkTemplate(String templateId) throws IOException {
		return xstoreVodClient.getWatermarkTemplateClient().getWatermarkTemplate(templateId);
	}

	/**
	 * 删除水印模板
	 *
	 * @param templateId 指定删除的水印模板的id
	 * @return DeleteWatermarkTemplateResponse, 删除指定水印模板的返回类，只包含code和message
	 * @throws IOException 可能抛出IO异常
	 */
	@PostMapping(value = "/watermark/delete")
	public Object deleteWatermarkTemplate(String templateId) throws IOException {
		return xstoreVodClient.getWatermarkTemplateClient().deleteWatermarkTemplate(templateId);
	}
}
